﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.IO;

namespace ADECMS.UserPages
{
    public partial class PresentationView : System.Web.UI.Page
    {
        ADECMSDBDataContext cmsdb;
        static string[] fajlovi;
        static int nFajlovi;
        static int brojac = 0;
        static string prezentacija;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cmsdb = new ADECMSDBDataContext();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            Image1.Visible = false;
            btnNext.Visible = false;
            btnPrev.Visible = false;
            if (!Page.IsPostBack)
            {
                HttpCookie loginCookie = Request.Cookies["AuthCookie"];
                if (Request.IsAuthenticated)
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(loginCookie.Value);
                    Label5.Text = "Logovani ste " + ticket.Name.ToString();

                    var useri = from k in cmsdb.USERs
                                from p in cmsdb.USER_TYPEs
                                where k.username == ticket.Name && k.user_type_id == p.id && p.type == "Administrator"
                                select k;
                    try
                    {
                        USER kor = useri.First();
                        MenuItem apanel = new MenuItem("Admin panel");
                        Menu1.Items.Add(apanel);
                    }
                    catch (System.InvalidOperationException)
                    {
                        return;
                    }
                }
            }
            
        }

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            switch (e.Item.Text)
            {
                case "Sign out":
                    FormsAuthentication.SignOut();
                    Response.Redirect("/Default.aspx");
                    break;
                case "Dodaj novost":
                    Response.Redirect("/UserPages/AddNews.aspx");
                    break;
                case "Pretraga":
                    Response.Redirect("/UserPages/Search.aspx");
                    break;
                case "Admin panel":
                    Response.Redirect("/UserPages/AdminPanel.aspx");
                    break;
                case "File upload":
                    Response.Redirect("/UserPages/FileUpload.aspx");
                    break;
                case "File download":
                    Response.Redirect("/UserPages/FileDownload.aspx");
                    break;
                case "Upload prezentacije":
                    Response.Redirect("/UserPages/PresentationUpload.aspx");
                    break;
                case "Pregled prezentacije":
                    Response.Redirect("/UserPages/PresentationView.aspx");
                    break;
                case "Pregledaj profil":
                    Response.Redirect("/UserPages/ProfileView.aspx");
                    break;
                case "Edituj profil":
                    Response.Redirect("/UserPages/ProfileEdit.aspx");
                    break;
            }

        }

        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UserPages/Start.aspx");
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                
                FILE fil = cmsdb.FILEs.Single(f => f.name == GridView1.SelectedRow.Cells[1].Text && f.type == "prezentacija");
                string filepath = HttpContext.Current.Server.MapPath("/Uploads/Files/" + Path.GetFileNameWithoutExtension(fil.name) + "/");
                System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(filepath);
                fajlovi = Directory.GetFiles(filepath);
                nFajlovi = dInfo.GetFiles().GetUpperBound(0) + 1;
                brojac = 0;
                prezentacija = fil.name;
                Image1.ImageUrl = "SlideHandler.ashx?prez=" + prezentacija + "&broj=" + Convert.ToString(brojac);
                Image1.Visible = true;
                btnNext.Visible = true;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = "SlideHandler.ashx?prez=" + prezentacija + "&broj=" + Convert.ToString(++brojac);
            btnPrev.Visible = true;
            Image1.Visible = true;
            if (brojac == nFajlovi-1) btnNext.Visible = false;
            else btnNext.Visible = true;
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = "SlideHandler.ashx?prez=" + prezentacija + "&broj=" + Convert.ToString(--brojac);
            Image1.Visible = true;
            if (brojac == 0) btnPrev.Visible = false;
            else btnPrev.Visible = true;
            btnNext.Visible = true;
        }
    }
}
